from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from cdsay.upimage.models import modelImage

import csv
import StringIO
        
def upload(request):
    file_obj = request.FILES.get('file', None)
    if file_obj:
        buf = StringIO.StringIO(file_obj)
        #buf=StringIO.StringIO(file_obj['content']
        try:
            #reader = csv.reader(buf)
            reader = csv.reader(buf)
        except:
            return render_to_response('upimage.html',
           {'message':'The file is must.'})
        else:
            for row in reader:
                objs = modelImage.objects
                #objs.get_empty_query_set()
                #objs.save()
                obj = objs.get_empty_query_set()#objs[0]
                obj.gender = row[1]
                obj.telphone = row[2]
                #obj.mobile = row[3]
                #obj.room = row[4]
                obj.update(obj,request.FILES)
                return HttpResponseRedirect('/address/')
            else:
                return render_to_response('upimage.html',
                                          {'message':'The file is must.'})
    else:
        return render_to_response('upimage.html', {'form': ''})